<style>

.coefficient span.error {
    position: absolute !important;
    left:150px;
}

#coefficientsTBody tr td div {
    height: 29px;
    padding-top: 3px;
}

.quantityError{
    border-color:#e9573f;
}
#coefficientsTable .selected input.textInput{
    background:#d2eafa !important;
}
 
</style>
<link href="${rc.getContextPath()}/manage/css/dwzCustomCss/dwz.custom.css?v=${ver}" rel="stylesheet" type="text/css">
<form method="post" action="${rc.getContextPath()}/manage/wcStandardCapacity/saveWCStandardCapacity" class="pageForm required-validate" onsubmit="return coefficientJs._tsubmit(this);">
    <div class="pageContent">
        <div class="pageFormContent"  >
        <input type="hidden" name="navTabId" value="${navTabId!''}">
        <input type="hidden" name="id" value="<#if (wcStandardCapacity.workCenterCode)??>${wcStandardCapacity.workCenterCode!''}</#if>">
        <fieldSet>
            <dl>
                <dt><@label key="workCenter.C,colon">工作中心：</@label></dt>
                
                <#if (wcStandardCapacity.workCenterCode)??>
                    <dd>
                        <input type="text" id="workCenterGrp.workCenterCode" name="workCenterCode" readonly="readlony"  value="${wcStandardCapacity.workCenterCode!''}"
                         lookupGroup="workCenterGrp" class="required textInput"/>
                    </dd>
                <#else>
                    <dd>
                        <input type="text" id="workCenterGrp.workCenterCode" name="workCenterCode" postField="workCenterCode" suggestFields="workCenterCode,workCenterText" 
                        suggestUrl="${rc.getContextPath()}/manage/wcStandardCapacity/selectUnusedWorkCenter" warn="<@label key='workCenterNotExitstOrIsInUse.C'>工作中心不存在或已被使用</@label>"
                        remote="${rc.getContextPath()}/manage/wcStandardCapacity/checkUnusedWorkCenter"  lookupGroup="workCenterGrp" class="required textInput"/>
                    </dd>
                </#if>
            </dl>
            <dl>
                <dt><@label key="description.C,colon">描述：</@label></dt>
                <dd>
                    <input type="text" id="workCenterGrp.workCenterText" name="workCenterText" value="${wcStandardCapacity.workCenterText!''}"readonly="readonly"/>
                </dd>
            </dl>
            <dl>
                <dt><@label key="standardCapacity.C,colon">标准产能</@label></dt>
                <dd>
                    <input type="text" name="standardCapacity" value="${wcStandardCapacity.standardCapacity!''}" class="required digits" min="0" maxlength="9"/>
                </dd>
            </dl>
            <dl>
                <dt><@label key="standardHours.C,colon">标准用时(小时)</@label></dt>
                <dd>
                    <input type="text" name="standardHours" value="${wcStandardCapacity.standardHours!''}" class="required digits" min="0" maxlength="9"/>
                </dd>
            </dl>
        </fieldSet>
        <div class="panel" style="width:100%;margin:0 0 5px 0;">
                <h1><@label key="capacityCoefficient.C">产能系数</@label></h1>
                    <div>
                        <div class="pBar">
                            <ul class="toolBar">
                                <li><a class="add" href="javascript:void(0);" onclick="coefficientJs._addRow();"> <span><@label key="add.B">新增</@label></span> </a></li>
                                <li><a class="delete" href="javascript:void(0);"  onclick="coefficientJs._deleteTr()"><span><@label key="delete.B">删除</@label></span> </a>
                                </li>
                            </ul>
                        </div>
                        <table id="coefficientsTable" class="table border canopration" width="100%" <#if (wcStandardCapacity.workCenterCode)??>layoutH="310"<#else>layoutH="230" </#if>>
                            <thead>
                                <tr class="theadTr">
                                    <th width="30"><input group="ids" type="checkbox" class="checkboxCtrl"></th>
                                    <th width="100"><@label key="lowerQuantity.C">下限量</@label></th>
                                    <th width="100"><@label key="upperQuantity.C">上限量</@label></th>
                                    <th width="100"><@label key="capacityCoefficient.C">产能系数</@label></th>
                                </tr>
                            </thead>
                            <tbody id="coefficientsTBody">
                                <#if wcStandardCapacity.capacityCoefficients?? && (wcStandardCapacity.capacityCoefficients?size>0)>
                                <#list wcStandardCapacity.capacityCoefficients as capacityCoefficient>
                                <tr>
                                    <td width="30"><input  name="ids" type="checkbox" class="checkboxCtrl"></td>
                                    <td width="100" class="coefficient"><input name="capacityCoefficients[${capacityCoefficient_index}].lowerQuantity" value="${capacityCoefficient.lowerQuantity!''}" type="text" class="textInput digits lower" /></td>
                                    <td width="100" class="coefficient"><input name="capacityCoefficients[${capacityCoefficient_index}].upperQuantity" value="${capacityCoefficient.upperQuantity!''}" type="text" class="textInput digits upper" /></td>
                                    <td width="100" class="coefficient required"><input name="capacityCoefficients[${capacityCoefficient_index}].coefficient" value="${capacityCoefficient.coefficient!''}" type="text" class="textInput number required" min="0" /></td>
                                </tr>
                                </#list>
                            </#if>
                            </tbody>
                        </table>
                    </div>
            </div> 
        </div>
    <#if (wcStandardCapacity.workCenterCode)??>
            <#assign createInfo=wcStandardCapacity>
            <#include "/common/createInfo.html">
    </#if>
    <div class="formBar">
            <ul>
                <li>
                    <div class="buttonActive" id="save">
                        <div class="buttonContent">
                            <button type="submit"><@label key="save.B">保存</@label></button>
                        </div>
                    </div>
                    </li>
                <li>
                    <div class="buttonActiveGrey">
                        <div class="buttonContent">
                            <button type="button" class="close">
                                    <@label key="close.B">关闭</@label>
                            </button>
                        </div>
                    </div>
                </li>
            </ul>
    </div>
    
<script>

    var $dialog=$.pdialog.getCurrent();
    var $tbody=$("#coefficientsTBody",$dialog);
    var trNum=$tbody.find("tr").length;
    var gridTh=$("#coefficientsTable",$dialog).find("tr.theadTr th");
    var getTrHtml=function(index){
        return '<tr class="trInput empty">'
        +'<td style="width:'+$(gridTh[0]).width()+'px;"><div><input name="ids" type="checkbox" class="checkboxCtrl"/></div></td>'
        +'<td style="width:'+$(gridTh[1]).width()+'px;"><div><input name="capacityCoefficients['+index+'].lowerQuantity" type="text" maxlength="9" class="textInput digits lower" /></div></td>'
        +'<td style="width:'+$(gridTh[2]).width()+'px;"><div><input name="capacityCoefficients['+index+'].upperQuantity" type="text" maxlength="9" class="textInput digits upper" /></div></td>'
        +'<td style="width:'+$(gridTh[3]).width()+'px;" class="coefficient required"><div><input name="capacityCoefficients['+index+'].coefficient" maxlength="9" type="text" class="textInput number" min="0" /></div></td>'
        +'</tr>';
    }
    var myFunction={
            _compare:function(value1,value2){
                value1=Number(value1);
                value2=Number(value2);
                if(value1<value2)
                    return -1;
                else if(value1>value2)
                    return 1;
                else
                    return 0;
            },
            
            _detectRepeat:function(arr){
                var hash={};
                for(var i in arr){
                    if(hash[arr[i]])
                        return true;
                    hash[arr[i]]=true;
                }
                return false;
            },
            
            _showClass:function(o,clazz){
                for(var i in o)
                {
                    o[i].addClass(clazz);
                }
            },
            
            _hideClass:function(o,clazz){
                for(var i in o)
                {
                    o[i].removeClass(clazz);
                }
            }
            
    }
    
    $tbody.on("keydown","tr.empty",function(){
        $tr=$(this);
        var empty=true;
        $tr.find("input.textInput").each(function(i){
            if($(this).val().length==0){
                empty=false;
                $tr.removeClass("empty");
                $tr.find("td.coefficient.required input").addClass("required");
                return;
            }
        })
        if(!empty)
        {
            trNum++;
            $tbody.append(getTrHtml(trNum));
        }
        
    });
    
    $tbody.on("change","tr:not(.empty)",function(){
        $tr=$(this);
        var lower=$tr.find(".lower");
        var upper=$tr.find(".upper");
        if(lower.val()!=''
                && upper.val()!=''
                    && Number(lower.val())>=Number(upper.val()))
        {
            myFunction._showClass([lower,upper],"quantityError");
            alertMsg.warn("<@label key='lowerQuantityShouldBiggerThanUpperQuantity.C'>上限量应大于下限量</@label>");
        }
        else
        {
            myFunction._hideClass([lower,upper],"quantityError");
        }
        
    });
        
    var coefficientJs={
            _addRow:function(){
                $tbody.append(getTrHtml(trNum));
            },
            _deleteTr:function(){
          	  var count = 0;
        	  $tbody.find("input:checked").each(function(i){
                  count++;
               });
       	    if (count == 0) {
       	        alertMsg.warn('<@label key="selected_first.C,capacityCoefficient.C">请选择产能系数！</@label>!')
       	    }else{
       	    alertMsg.confirm("<@label key='delete_sure.D,'>确定要删除？</@label>?", {
   	            okCall : function() {
   	            	   $tbody.find("input:checked").each(function(i){
   	                       $(this).parents("tr").remove();
   	                   })
   	            }
   	        });}
            },
            _tsubmit : function(t) {
                var $form = $(t);
                if (!$form.valid() || $tbody.find(".quantityError").length>0) {
                    return false;
                }
                var emptyLowerQuantity=0;
                var emptyUpperQuantity=0;
                var valid1=true;
                var valid2=true;
                var quantityArr=[];
                $tbody.find("tr:not(.empty)").each(function(i){
                    var lower=$(".lower",$(this));
                    var upper=$(".upper",$(this));
                    var lowerLen=lower.val().replace(/\s/g, "").length;
                    var upperLen=upper.val().replace(/\s/g, "").length;
                    if(lowerLen==0 && upperLen!=0)
                    {
                        if(++emptyLowerQuantity>1)
                        {
                            valid1=false;
                            return false;
                        }
                        quantityArr.push(upper.val());
                        quantityArr.push(Number.NEGATIVE_INFINITY);
                    }
                    else if (lowerLen!=0 && upperLen==0)
                    {
                        if(++emptyUpperQuantity>1)
                        {
                            valid1=false;
                            return false;
                        }
                        quantityArr.push(Number.POSITIVE_INFINITY);
                        quantityArr.push(lower.val());
                    }
                    else
                    {
                        quantityArr.push(lower.val());
                        quantityArr.push(upper.val());
                    }
                    if(myFunction._detectRepeat(quantityArr)==true){
                        valid2=false;
                        myFunction._showClass([lower,upper],"quantityError");
                        return false;
                    }
                    quantityArr.sort(myFunction._compare);
                    if(quantityArr.indexOf(lower.val())%2===1||quantityArr.indexOf(upper.val())%2===0)
                    {
                        valid2=false;
                        myFunction._showClass([lower,upper],"quantityError");
                        return false;
                    }
                })
                if(!valid1){
                     alertMsg.warn("<@label key='lowerQuantityOrUpperQuantityAllowsLessThanOne.C'>只能有一行允许上限量或下限量为空！</@label>");
                    return false; 
                }
                if(!valid2){
                     alertMsg.warn("<@label key='quantityHasRepeteNumbers.C'>限量范围有重叠！</@label>");
                    return false; 
                }
                return validateCallback(t, dialogAjaxDone);
            }
    };


</script>            
            